package cn.itsource.controller.blog;


import cn.itsource.annotation.PreAuthorize;
import cn.itsource.blog.domain.article.Article;
import cn.itsource.blog.domain.article.ArticleDto;
import cn.itsource.blog.domain.article.CountTime;
import cn.itsource.blog.domain.article.DivTime;
import cn.itsource.blog.query.article.ArticleQuery;
import cn.itsource.head.ColPic;
import cn.itsource.head.PiePic;
import cn.itsource.iploading.BlogArticleService;
import cn.itsource.service.blog.IArticleService;
import cn.itsource.utils.JsonResult;
import com.github.pagehelper.PageInfo;
import io.minio.errors.*;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.List;

@RestController
@RequestMapping("/article")
public class ArticleController {
    @Autowired
    private IArticleService articleService;

    @Autowired
    private BlogArticleService blogArticleService;

    @GetMapping
    public List<Article> select(){
        return articleService.selectAll();
    }

    @GetMapping("/{id}")
    public Article selctOne(@PathVariable("id") Integer id){
        return articleService.selectOne(id);
    }

    @GetMapping("/getDependencies/{id}")
    public Article selectDepend(@PathVariable("id") Integer id){
        return articleService.selectDepend(id);
    }

    @PatchMapping
    public JsonResult deleteMore(@RequestBody Long[] ids){
        try {
            articleService.deleteMore(ids);
            return JsonResult.success("删除用户数据成功！");
        }catch (Exception e){
            e.printStackTrace();
            return JsonResult.error();
        }
    }
    //获取分页
    @PostMapping
    public PageInfo<Article> queryPage(@RequestBody ArticleQuery articleQuery){
        return articleService.queryArticle(articleQuery);
    }

    @DeleteMapping("/{id}")
    @PreAuthorize(name = "文章删除", sn = "article:save",descs = "对文章删除")
    public JsonResult delete(@PathVariable("id") Integer id) throws ServerException, InvalidBucketNameException, InsufficientDataException, ErrorResponseException, IOException, NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException, XmlParserException, InternalException {
        articleService.delete(id);
        return JsonResult.success("删除用户数据成功！");
    }

    @PutMapping
    public JsonResult update(@RequestBody Article article) {

        articleService.update(article);
        return JsonResult.success("修改用户数据成功！");
    }

    @GetMapping("/dto")
    public List<ArticleDto> selectDto(){
        return articleService.selectDto();
    }

    @GetMapping("/time")
    public List<CountTime> selectTime(){
        return articleService.selectTime();
    }

    @PostMapping("/div")
    public List<DivTime> selectDiv(@RequestBody CountTime countTime){
        return articleService.selectDiv(countTime);
    }
    // 饼状图
    @GetMapping("/pie")
    public List<PiePic> selectPie(){
        return articleService.selectPie();
    }
    //条形图
    @GetMapping("/col")
    public List<ColPic> selectCol(){
        return articleService.selectCol();
    }
    //阅读量
    @PutMapping("/up")
    public JsonResult up(@RequestBody Article article){
        articleService.readUp(article);
        return JsonResult.success("成功");
    }
    /**
     * 前台系统点赞功能接口
     * @param id
     * @param request
     * @return
     */
    @PutMapping("/updateStar/{id}")
    @ApiOperation(value = "前台系统 - 点赞功能")
    public JsonResult updateStar(@PathVariable("id") Integer id, HttpServletRequest request){
        try {
            return blogArticleService.updateStar(id,request);
        } catch (Exception e) {
            e.printStackTrace();//打印错误信息
            return  JsonResult.error("异常");
        }
    }

    /**
     * 业务接口：前台点赞图片样式加载
     * @param id
     * @param request
     * @return
     */
    @GetMapping("/initIconClass/{id}")
    @ApiOperation(value = "前台系统 - 页面加载点赞图片样式")
    public JsonResult initIconClass(@PathVariable("id")Integer id,HttpServletRequest request){
        try {
            return blogArticleService.initIconClass(id,request);

        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.error("网络异常，请稍后再试！！！");
        }
    }














}

